<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        var obj = {
            name: "张三",
            tel: {
                num: 110,
                type: "中国移动",
                component: {
                    type: "股份有限公司",
                    money: 100
                }
            }
        } 

        // var obj2 = obj
        // obj2.tel.type = "中国联通"
        // console.log(obj)
        // var obj2 = {}
        // for(var key in obj) {
        //     obj2[key] = obj[key]
        // }
        // obj2.name = "李四"
        // console.log(obj)

        // var obj2 = Object.assign({}, obj)
        // obj2.tel.type = "中国联通"
        // console.log(obj)
        // var str = '134'
        // var str2 = str
        // str2 = '345'
        // console.log(str)
        // 第一种深复制的方法 就是转化为json
        // var json = JSON.stringify(obj)
        // var obj2 = JSON.parse(json) 
        
        // obj2.tel.type = "中国联通"
        // console.log(obj, obj2)
        // 第二种方法就是递归 递归就是函数的自我调用
        // function show() {
        //     show()
        // }

        function deepClone(params) {
            let obj = {}
            if(params instanceof Object) {
                for(let key in params) {
                    if(params[key] instanceof Object) {
                       obj[key] = deepClone(params[key])
                    } else {
                        obj[key] = params[key]
                    }
                }
            }
            return obj
        }

        var obj2 = deepClone(obj)
        obj2.tel.component.money = "200"
        console.log(obj, obj2)

        var arr = [1, [2, [3, 4]]];

        function flatten(arr) {
            var result = [];
            for (var i = 0, len = arr.length; i < len; i++) {
                if (Array.isArray(arr[i])) {
                    result = result.concat(flatten(arr[i]))
                }
                else {
                    result.push(arr[i])
                }
            }
            return result;
        }


        console.log(flatten(arr))
    </script>
</body>
</html>